package creditcard.gui;

import java.util.Map;
import java.util.Vector;

import creditcard.Model;
import framework.account.IAccount;

/**
 * @author Elbek Kamoliddinov
 */
public class Controller {

    /**
     * @uml.property name=""
     * @uml.associationEnd multiplicity = ""
     */
    Model myModel;
    /**
     * @uml.property name=""
     * @uml.associationEnd multiplicity=""
     */
    View view;

    public Controller(View view) {
        this.view = view;
        this.myModel = Model.getInstance();
        view.addListener(new SymAction());
        view.addListenerToWindow(new SymWindow());
    }

    class SymWindow extends java.awt.event.WindowAdapter {
        public void windowClosing(java.awt.event.WindowEvent event) {
            Object object = event.getSource();
            if (object == view)
                BankFrm_windowClosing(event);
        }
    }

    void BankFrm_windowClosing(java.awt.event.WindowEvent event) {
        this.exitApplication();
    }

    class SymAction implements java.awt.event.ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent event) {
            Object object = event.getSource();
            if (object == view.JButton_Exit)
                JButtonExit_actionPerformed(event);
            else if (object == view.JButton_NewCCAccount)
                JButtonNewCCAC_actionPerformed(event);
            else if (object == view.JButton_GenBill)
                JButtonGenerateBill_actionPerformed(event);
            else if (object == view.JButton_Deposit)
                JButtonDeposit_actionPerformed(event);
            else if (object == view.JButton_Withdraw)
                JButtonWithdraw_actionPerformed(event);

        }
    }

    // When the Exit button is pressed this code gets executed
    // this will exit from the system
    void JButtonExit_actionPerformed(java.awt.event.ActionEvent event) {
        System.exit(0);
    }

    void JButtonNewCCAC_actionPerformed(java.awt.event.ActionEvent event) {
        /*
           * JDialog_AddPAcc type object is for adding personal information
           * construct a JDialog_AddPAcc type object set the boundaries and show
           * it
           */
        JDialog_AddCCAccount ccac = new JDialog_AddCCAccount(view);
        ccac.setBounds(450, 20, 300, 380);
        ccac.show();

        if (!ccac.isCreated())
            return;
        Map<String, String> cusMap = ccac.getCustomerMap();
        Vector<String> row = new Vector<String>();

        row.add(cusMap.get("name"));
        row.add(cusMap.get("ccNumber"));
        row.add(cusMap.get("expDate"));
        row.add(cusMap.get("type"));
        row.add(cusMap.get("balance"));
        view.getModel().addRow(row);
        Model.getInstance().addRow(cusMap);
        view.JTable1.getSelectionModel().setAnchorSelectionIndex(-1);
    }

    void JButtonGenerateBill_actionPerformed(java.awt.event.ActionEvent event) {

    }

    void JButtonDeposit_actionPerformed(java.awt.event.ActionEvent event) {
        // get selected name
        int selection = view.JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection == -1)
            return;
        view.setSelection(selection);
        IAccount account = Model.getInstance().getSelectedAccount(selection);
        JDialog_Deposit depositDialog = new JDialog_Deposit(view, account);
        depositDialog.setBounds(450, 20, 300, 380);
        depositDialog.show();

    }

    void JButtonWithdraw_actionPerformed(java.awt.event.ActionEvent event) {

        int selection = view.JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection == -1)
            return;
        view.setSelection(selection);
        IAccount account = Model.getInstance().getSelectedAccount(selection);
        JDialog_Withdraw withdrawDialog = new JDialog_Withdraw(view, account);
        withdrawDialog.setBounds(450, 20, 300, 380);
        withdrawDialog.show();
    }

    void exitApplication() {
        try {
            view.setVisible(false); // hide the Frame
            view.dispose(); // free the system resources
            System.exit(0); // close the application
        } catch (Exception e) {
        }
    }
}
